#include "gamecamera.h"

CGameCamera::CGameCamera()
{
	
	m_pTargetEntity = NULL;
	
	m_MaxCameraSpeed = 100.0;
	m_CameraDecel = 50.0;
	m_CameraAccel = 0.0;
	
	m_bIncludeOutsideSpace = false;
	m_bAlwaysCentered = false;
	
	m_bLocked = false;
	
	m_TargetType = TARGET_POS;
	m_MovementType = MOVEMENT_INSTANT;
	
	m_FocusPadding = 70.0;
	
}

void CGameCamera::Translate( double dps )
{
	
	if( !m_bLocked )
	{
	
		bool updateCamera = false;
		bool updateX = false;
		bool updateY = false;
		
		Vector2D cameraCenter = m_Translate;
		cameraCenter.x += SCREEN_WIDTH * .5;
		cameraCenter.y += SCREEN_HEIGHT * .5;
		
		Vector2D targetPos = m_TargetPos;
		
		if( m_TargetType == TARGET_ENTITY )
		{
			
			if( m_pTargetEntity )
			{ 
				
				Vector2D size = m_pTargetEntity->GetSize();
				
				targetPos = m_pTargetEntity->GetPos();
				targetPos.x += size.x * .5;
				targetPos.y += size.y * .5;
				
			}
			
		}
		
		double padding = m_FocusPadding;
		
		if( m_bAlwaysCentered )
		{
			
			padding = 0.0;
			
		}
		
		if( cameraCenter.x < targetPos.x - padding ||
			cameraCenter.x > targetPos.x + padding )
		{
			
			updateCamera = true;
			updateX = true;
			
		}
		
		if( cameraCenter.y < targetPos.y - padding ||
			cameraCenter.y > targetPos.y + padding )
		{
			
			updateCamera = true;
			updateY = true;
			
		}
		
		if( updateCamera )
		{
			
			switch( m_MovementType )
			{
				
				case MOVEMENT_INSTANT:
					if( m_bAlwaysCentered )
					{
						
						m_Translate.x = targetPos.x - SCREEN_WIDTH * .5;
						m_Translate.y = targetPos.y - SCREEN_HEIGHT * .5;
						
					} else
					{
						
						if( updateX )
						{
							
							if( targetPos.x > cameraCenter.x + padding )
								m_Translate.x += ( targetPos.x - ( cameraCenter.x + padding ) );
							
							if( targetPos.x < cameraCenter.x - padding )
								m_Translate.x += ( targetPos.x - ( cameraCenter.x - padding ) );
							
						}
						
						if( updateY )
						{
							
							
							if( targetPos.y > cameraCenter.y + padding )
								m_Translate.y += ( targetPos.y - ( cameraCenter.y + padding ) );
							
							if( targetPos.y < cameraCenter.y - padding )
								m_Translate.y += ( targetPos.y - ( cameraCenter.y - padding ) );
							
						}
						
					}
					break;
				case MOVEMENT_VELOCITY:
					double xmul = 1.0, ymul = 1.0;
					
					if( cameraCenter.x > targetPos.x )
						xmul = -1.0;
					
					if( cameraCenter.y > targetPos.y )
						ymul = -1.0;
					
					if( updateX )
						m_Translate.x += m_MaxCameraSpeed * xmul * dps;
					
					if( updateY )
						m_Translate.y += m_MaxCameraSpeed * ymul * dps;
					
					break;
					
			}
			
		}
		
		if( !m_bIncludeOutsideSpace )
		{
			
			if( m_BottomRight.x > SCREEN_WIDTH )
			{
				
				if( m_Translate.x < m_TopLeft.x )
					m_Translate.x = m_TopLeft.x;
				
				if( m_Translate.x + SCREEN_WIDTH > m_BottomRight.x )
					m_Translate.x = m_BottomRight.x - SCREEN_WIDTH;
				
			}
			
			if( m_BottomRight.y > SCREEN_HEIGHT )
			{
				
				if( m_Translate.y < m_TopLeft.y )
					m_Translate.y = m_TopLeft.y;
				
				if( m_Translate.y + SCREEN_HEIGHT > m_BottomRight.y )
					m_Translate.y = m_BottomRight.y - SCREEN_HEIGHT;
				
			}
			
		}
	
	}
			
	Draw::Translate( -m_Translate.x, -m_Translate.y, 0.0f );
}